import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:memo/model/task.dart';
import 'package:memo/utils/toast.dart';
import 'package:memo/service/task_dao.dart';

import 'detail_task.dart';

class SearchTaskPage extends StatefulWidget {
  @override
  _SearchTaskPageState createState() => _SearchTaskPageState();
}

class _SearchTaskPageState extends State<SearchTaskPage> {
  List<Task> tasks = [];
  TaskDao taskDao;
  TextEditingController searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    taskDao = TaskDao();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Container(
          decoration: new BoxDecoration(
            border: Border.all(color: Colors.grey, width: 1.0), //灰色的一层边框
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(30.0)),
          ),
          alignment: Alignment.center,
          height: 40,
          child: TextField(
            controller: searchController,
            decoration: InputDecoration(
                contentPadding: EdgeInsets.only(left: 8.0,bottom: 5.0),
              ),
            autofocus: true,
          ),
        ),
        actions: <Widget>[
          InkWell(
            child: Container(
              width: 50,
              height: 50,
              child: Center(
                child: Text("搜索"),
              ),
            ),
            onTap: () {
              if (searchController.text.length > 0) {
                taskDao.taskSearch(searchController.text).then((value) {
                  setState(() {
                    tasks = value;
                  });
                });
              }
            },
          )
        ],
      ),
      body: ListView.separated(
        itemCount: tasks.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            onTap: () => _jumpToDetailPage(context, index),
            title: _buildContent(index),
            subtitle: Text('截止时间:${tasks[index].endTimeStr}'),
            leading: _buildIsFinish(index),
            trailing: _buildDelete(index),
          );
        },
        separatorBuilder: (BuildContext context, int index) => Divider(
          color: Colors.grey[600],
        ),
      ),
    );
  }

  void _jumpToDetailPage(context, index) {
    Navigator.of(context)
        .push(MaterialPageRoute(
            builder: (ctx) => DetailPage(taskDao, tasks[index])))
        .then((res) {
      taskDao.tasks().then((value) {
        setState(() {
          tasks = value;
        });
      });
    });
  }

  Widget _buildContent(index) {
    return Text(
      tasks[index].content,
      style: TextStyle(fontSize: 18),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  Widget _buildIsFinish(index) {
    return GestureDetector(
      child: tasks[index].isFinish
          ? Icon(Icons.check_circle)
          : Icon(Icons.check_circle_outline),
      onTap: () {
        tasks[index].isFinish = !tasks[index].isFinish;
        taskDao.updateTask(tasks[index]);
        setState(() {});
      },
    );
  }

  Widget _buildDelete(index) {
    return IconButton(
      icon: Icon(Icons.delete),
      onPressed: () {
        taskDao.deleteTask(tasks[index].id);
        setState(() {
          tasks.removeAt(index);
        });
        Toast.toast(context, msg: "删除成功！", position: ToastPostion.top);
      },
    );
  }
}
